﻿using AMAP.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace AMAP.Areas.Admin.Models.Basket
{
    public class AdminBasketCreateViewModel
    {
        public AdminBasketCreateViewModel()
        {
            this.Stock = new List<StockItem>();
        }

        /// <summary>
        /// Initialize the stock list with given internal and external stock
        /// </summary>
        /// <param name="InternalStock">The list of remaining products of passed commands</param>
        /// <param name="ExternalStock">The list of remaining external buys done to other amaps</param>
        /// <remarks>Internal stock comes from Order and External from other AMAP</remarks>
        public AdminBasketCreateViewModel(IList<Order> InternalStock, IList<ExternalOrder> ExternalStock)
            : base()
        {
            this.Stock = new List<StockItem>();
            foreach (Order o in InternalStock)
                this.Stock.Add(new StockItem
                {
                    Id = o.Id,
                    Type = typeof(Order).FullName,
                    ExpirationDate = o.Offer.HarvestDate.AddDays(o.Offer.Product.ShelfLife),
                    Name = o.Offer.Product.Name,
                    Price = o.Offer.Price,
                    Product = o.Offer.Product,
                    Quantity = o.RemainedQuantity,
                    Unit = o.Offer.Unit
                });
            foreach (ExternalOrder e in ExternalStock)
                this.Stock.Add(new StockItem
                {
                    Id = e.Id,
                    Type = typeof(ExternalOrder).FullName,
                    ExpirationDate = e.HarvestDate.AddDays(e.Product.ShelfLife),
                    Name = e.Product.Name,
                    Price = e.Price,
                    Product = e.Product,
                    Quantity = e.RemainedQuantity,
                    Unit = e.Unit
                });
        }

        public IList<StockItem> Stock { get; set; }
        public IList<Offer> NewOffers { get; set; }
        public IList<ExternalOffer> ExternalOffers { get; set; }
        public int NumberOfBasket { get; set; }
        public ICollection<StockResponse> StockUsage { get; set; }
        public ICollection<OfferResponse> OfferUsage { get; set; }
        public ICollection<ExternalResponse> ExternalUsage { get; set; }

        [Required(ErrorMessage = "Vous devez spécifier le prix du panier")]
        [Display(Name = "Prix du panier pour les commandes extérieures")]
        public double? BasketPrice { get; set; }

        public class StockItem
        {
            public int Id { get; set; }
            public string Type { get; set; }
            public DateTime ExpirationDate { get; set; }
            public string Name { get; set; }
            public double Price { get; set; }
            public Product Product { get; set; }
            public double Quantity { get; set; }
            public string Unit { get; set; }
        }

        public class ExternalOffer
        {
            public int AmapId { get; set; }
            public int OfferId { get; set; }
            public double Quantity { get; set; }
            public double Price { get; set; }
            public string Unit { get; set; }
            public string Name { get; set; }
        }

        public class StockResponse
        {
            public int StockId { get; set; }
            public string Type { get; set; }
            public double Quantity { get; set; }
        }

        public class OfferResponse
        {
            public int OfferId { get; set; }
            public double Unit { get; set; }
            public double Quantity { get; set; }
        }

        public class ExternalResponse
        {
            public int AmapId { get; set; }
            public int OfferId { get; set; }
            public double Unit { get; set; }
            public double Quantity { get; set; }
        }
    }
}